package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.DepartmentalStructure;
import org.example.entity.Employee;
import org.example.entity.SalaryStructure;
import org.example.entity.SecurityProvident;
import org.example.models.R;
import org.example.service.EmployeeService;
import org.example.service.SecurityProvidentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/salary/setup/risks")
public class SecurityProvidentController {
    @Autowired
    SecurityProvidentService securityProvidentService;
    @Autowired
    EmployeeService employeeService;
    @GetMapping()
    public R<Page<SecurityProvident>> page(
            @RequestParam(name="current",defaultValue = "1") int current,
            @RequestParam(name="size",defaultValue = "10") int size,
            @RequestParam(name="emp",required = false) String emp
    ){
        QueryWrapper<SecurityProvident> queryWrapper = new QueryWrapper<>();
        if(emp != null){
            QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>();
            employeeQueryWrapper.lambda().like(Employee::getEmployeeName, emp);

            // 假设你有EmployeeService，可以通过它查询员工
            List<Employee> employees = employeeService.list(employeeQueryWrapper);

            // 如果查询到员工，获取他们的ID并作为条件
            if(employees != null && !employees.isEmpty()){
                List<String> employeeIds = new ArrayList<>();
                for (Employee employee : employees) {
                    Integer employeeId = employee.getEmployeeId();
                    employeeIds.add(String.valueOf(employeeId));
                }
                queryWrapper.lambda().in(SecurityProvident::getEmp, employeeIds);
            } else {
                // 如果没有查询到员工，可以直接返回空结果
                return R.success(new Page<>());
            }
        }

        Page<SecurityProvident> page = new Page<>(current, size);

        securityProvidentService.page(page,queryWrapper);
        return R.success(page);

    }
    @PostMapping("/insert")
    public R insert(@RequestBody SecurityProvident securityProvident){
        securityProvidentService.insert(securityProvident);
        return R.success();
    }

    @PutMapping("/update")
    public R update(@RequestBody SecurityProvident securityProvident){
        securityProvidentService.updateByPrimaryKeySelective(securityProvident);
        return R.success();
    }
    @GetMapping("/list")
    public R<List<SecurityProvident>> page(
            @RequestParam(name="emp",required = false) String emp
    ){
        QueryWrapper<SecurityProvident> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SecurityProvident::getEmp, emp);
        List<SecurityProvident> securityProvidents = securityProvidentService.list(queryWrapper);

        // 如果查询不到该员工的记录，则查询emp为"默认"的记录
        if (securityProvidents == null || securityProvidents.isEmpty()) {
            queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(SecurityProvident::getEmp, "默认");
            securityProvidents = securityProvidentService.list(queryWrapper);
        }

        return R.success(securityProvidents);
    }
    @GetMapping("/ba")
    public R<List<SecurityProvident>> lis(
            @RequestParam(name="emp",required = false) String emp
    ){
        QueryWrapper<SecurityProvident> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SecurityProvident::getEmp,emp);
        List<SecurityProvident> securityProvidents = securityProvidentService.list(queryWrapper);
        return R.success(securityProvidents);
    }
}
